package com.xxx.dict.controller;

import com.github.pagehelper.PageInfo;
import com.xxx.base.utils.R;

import com.xxx.dict.domain.DictItem;
import com.xxx.dict.query.DictItemQuery;
import com.xxx.dict.service.IDictItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/DictItem")
@Api(tags = "数据字典明细类接口")
public class DictItemController {
    @Autowired
    private IDictItemService dictItemService;

    @GetMapping
    @ApiOperation(value = "查询所有")
    public R<List<DictItem>> finfAll(){
        List<DictItem> list = dictItemService.findAll();
        return R.ok(list);
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "ID查询单个")
    public R<DictItem> findOne(@PathVariable Long id) {
        DictItem DictItem = dictItemService.findOne(id);
        return R.ok(DictItem);
    }
    @PostMapping("/{id}")
    @ApiOperation(value = "分页查询")
    public PageInfo<DictItem> findByPage(@PathVariable Long id,@RequestBody DictItemQuery dictItemQuery) {
        List<DictItem> byPage = dictItemService.findByPage(dictItemQuery,id);
        PageInfo<DictItem> pageInfo = new PageInfo<>(byPage);
        return pageInfo;
    }

    @PutMapping
    @ApiOperation(value = "增加或者修改")
    public R<Void> updateOrInsert(@RequestBody DictItem dictItem){
        try {
            if (dictItem.getId()!=null){
                dictItemService.updateById(dictItem);
            }else {
                dictItemService.insert(dictItem);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("用户名重复");
        }
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除用户")
    public R<Void> delete(@PathVariable Long id){
        dictItemService.deleteById(id);
        return R.ok();
    }


    @PatchMapping
    @ApiOperation(value = "批量删除用户")
    public R<Void> bathDictItems(@RequestBody Long[] ids) {
        try {
            dictItemService.bathDictItems(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }
}
